package com.anti.modular.sys.log.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.modular.sys.log.entity.SysVisLog;
import com.anti.modular.sys.log.param.SysVisLogParam;
import com.anti.modular.sys.log.service.SysVisLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2021-3-1
 */
@Service
public class SysVisLogServiceImpl implements SysVisLogService {
    @Resource
    DBUtils dbUtils;

    @Override
    public PageResult page(PageParam pageParam, SysVisLogParam sysVisLogParam) {
        StringBuilder sb=new StringBuilder("select * from sys_vis_log where 1=1 ");
        ArrayList<Object> params = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(sysVisLogParam)) {
            //根据名称模糊查询
            if (ObjectUtil.isNotEmpty(sysVisLogParam.getName())) {
                sb.append(" and name like ?");
                params.add("%"+sysVisLogParam.getName()+"%");
            }
            //跟据访问类型（字典 1登入 2登出）查询
            if (ObjectUtil.isNotEmpty(sysVisLogParam.getVisType())) {
                sb.append(" and vis_type = ?");
                params.add(sysVisLogParam.getVisType());
            }
            //根据是否成功查询
            if (ObjectUtil.isNotEmpty(sysVisLogParam.getSuccess())) {
                sb.append(" and success = ?");
                params.add(sysVisLogParam.getSuccess());
            }
            // 根据时间范围查询
            if (ObjectUtil.isAllNotEmpty(sysVisLogParam.getSearchBeginTime(), sysVisLogParam.getSearchEndTime())) {
                sb.append(" and date_format (vis_time,'%Y-%m-%d') >= date_format('" + sysVisLogParam.getSearchBeginTime() + "','%Y-%m-%d')");
                sb.append(" and date_format (vis_time,'%Y-%m-%d') <= date_format('" + sysVisLogParam.getSearchEndTime() + "','%Y-%m-%d')");
            }
        }
        sb.append(" order by vis_time desc");
        return dbUtils.query(pageParam,sb.toString(),params.toArray());
    }

    @Override
    public void delete() {
        dbUtils.execSql("delete from sys_vis_log");
    }

    @Override
    public void save(SysVisLog sysVisLog) {
        dbUtils.insert(sysVisLog);
    }
}
